from abc import ABC, abstractmethod
from typing import Dict, Any, List
from dataclasses import dataclass
import logging

@dataclass
class AgentResult:
    """智能体执行结果标准格式"""
    success: bool
    data: Any
    metadata: Dict[str, Any] = None
    error: str = None

class BaseAgent(ABC):
    """智能体基类"""
    
    def __init__(self):
        self.logger = logging.getLogger(self.__class__.__name__)
    
    @abstractmethod
    def execute(self, input_data: Dict[str, Any]) -> AgentResult:
        """执行智能体任务"""
        pass
    
    @abstractmethod
    def get_name(self) -> str:
        """获取智能体名称"""
        pass
    
    @abstractmethod
    def get_version(self) -> str:
        """获取智能体版本"""
        pass
    
    def validate_input(self, input_data: Dict[str, Any]) -> bool:
        """验证输入数据"""
        return True
    
    def get_dependencies(self) -> List[str]:
        """获取依赖的智能体名称"""
        return []
    
    def log_execution_start(self):
        """记录执行开始"""
        self.logger.info(f"开始执行智能体: {self.get_name()}")
    
    def log_execution_end(self, success: bool, execution_time: float = None):
        """记录执行结束"""
        status = "成功" if success else "失败"
        time_info = f"，耗时: {execution_time:.2f}s" if execution_time else ""
        self.logger.info(f"智能体 {self.get_name()} 执行{status}{time_info}")
